// Copyright 2019 Lipeng Zha, Inc. All Rights Reserved.

#include "HotCreatorEditor.h"
#include "HotCreatorStyle.h"
#include "HotCreatorCommands.h"
#include "SHotCreator.h"

#include "Misc/MessageDialog.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "DesktopPlatformModule.h"
#include "LevelEditor.h"
#include "HAL/FileManager.h"
#include "Interfaces/IPluginManager.h"


static const FName HotCreatorTabName("HotCreator");

#define LOCTEXT_NAMESPACE "FHotCreatorEditorModule"

void FHotCreatorEditorModule::StartupModule()
{
	// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
	
	FHotCreatorStyle::Initialize();
	FHotCreatorStyle::ReloadTextures();

	FHotCreatorCommands::Register();
	
	PluginCommands = MakeShareable(new FUICommandList);

	PluginCommands->MapAction(
		FHotCreatorCommands::Get().PluginAction,
		FExecuteAction::CreateRaw(this, &FHotCreatorEditorModule::PluginButtonClicked),
		FCanExecuteAction());

	FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");

	{
		TSharedPtr<FExtender> MenuExtender = MakeShareable(new FExtender());
		MenuExtender->AddMenuExtension("WindowLayout", EExtensionHook::After, PluginCommands, FMenuExtensionDelegate::CreateRaw(this, &FHotCreatorEditorModule::AddMenuExtension));

		LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(MenuExtender);
	}

	 {
	 	TSharedPtr<FExtender> ToolbarExtender = MakeShareable(new FExtender);
	 	ToolbarExtender->AddToolBarExtension("Settings", EExtensionHook::After, PluginCommands, FToolBarExtensionDelegate::CreateRaw(this, &FHotCreatorEditorModule::AddToolbarExtension));

	 	LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(ToolbarExtender);
	 }
	FGlobalTabmanager::Get()->RegisterNomadTabSpawner(HotCreatorTabName, FOnSpawnTab::CreateRaw(this, &FHotCreatorEditorModule::OnSpawnPluginTab))
		.SetDisplayName(LOCTEXT("FHotCreatorTabTitle", "HotCreator"))
		.SetMenuType(ETabSpawnerMenuType::Hidden);

}

void FHotCreatorEditorModule::ShutdownModule()
{
	// This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
	// we call this function before unloading the module.
	FHotCreatorStyle::Shutdown();

	FHotCreatorCommands::Unregister();
	FGlobalTabmanager::Get()->UnregisterNomadTabSpawner(HotCreatorTabName);
}

void FHotCreatorEditorModule::PluginButtonClicked()
{
	FGlobalTabmanager::Get()->InvokeTab(HotCreatorTabName);
	// PrintUsageMsg();
}

void FHotCreatorEditorModule::PrintUsageMsg()
{
	UWorld* World = GEditor->GetEditorWorldContext().World();
	FText DialogText = FText::FromString(
		TEXT("This is a Test Message!")
	);
	FMessageDialog::Open(EAppMsgType::Ok, DialogText);
}


TSharedRef<class SDockTab> FHotCreatorEditorModule::OnSpawnPluginTab(const class FSpawnTabArgs& InSpawnTabArgs)
{
	return SNew(SDockTab)
		.TabRole(ETabRole::NomadTab)
		.Label(LOCTEXT("HotCreatorTab", "Hot Patcher"))
		.ToolTipText(LOCTEXT("WidgetGalleryTabTextToolTip", "Switch to the widget gallery."))
		.Clipping(EWidgetClipping::ClipToBounds)
		[
			// MakeWidgetGallery()
			SNew(SHotCreator)
		];
}

void FHotCreatorEditorModule::AddMenuExtension(FMenuBuilder& Builder)
{
	Builder.AddMenuEntry(FHotCreatorCommands::Get().PluginAction);
}



void FHotCreatorEditorModule::AddToolbarExtension(FToolBarBuilder& Builder)
{
	Builder.AddToolBarButton(FHotCreatorCommands::Get().PluginAction);
}

#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FHotCreatorEditorModule, HotCreatorEditor)